package com.efast.dmom.mgmt.service.mes.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.dao.mes.PmAutoPrintStationDao;
import com.efast.dmom.mgmt.entity.mes.PmAutoPrintStation;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.service.mes.PmAutoPrintStationService;
@Service
public class PmAutoPrintStationServiceImpl implements PmAutoPrintStationService {

	@Autowired
	private PmAutoPrintStationDao printStationDao;


//保存打印任务
	@Override
	public ResultBean savePrintTask(TwBasLot lot) {
		ResultBean result = new ResultBean(true, "'");
		result.setSuccess(true);
		Integer stationId = lot.getStationId();
		String vin = lot.getVin();
		String selfNumber = lot.getSelfNumber();
		PmAutoPrintStation pmAutoPrintStation = new PmAutoPrintStation();
		//判断vin
		if(vin==null||"".equals(vin)) {
			pmAutoPrintStation.setTaskNumber(selfNumber);
		}else {
			pmAutoPrintStation.setTaskNumber(vin);
		}
		//判断工位id
		if(stationId==null) {
			result.setSuccess(false);
			result.setMessage("无法获取工位ID");
			return result;	
		}	
		//判断成品物料号
		if(lot.getPartNoVersion()==null ||"".equals(lot.getPartNoVersion())) {
			result.setSuccess(false);
			result.setMessage("无法获取成品物料号");
			return result;	
		}
		//获取配置的打印工位信息
		pmAutoPrintStation.setSequence(lot.getSequence());
		pmAutoPrintStation.setOperation(lot.getOperation());
		pmAutoPrintStation.setOrderNumber(lot.getOrderNo());
		pmAutoPrintStation.setCreateuser(SpringWebUtils.getRequestUser());
		pmAutoPrintStation.setCreateTime(new Date());
		pmAutoPrintStation.setPrintStatus("1");//代表未打印
		pmAutoPrintStation.setPartNoVersion(lot.getPartNoVersion());
		List<PmAutoPrintStation> listPrintStation = printStationDao.getPrintStationInfoByStationId(stationId);
		if (listPrintStation == null || listPrintStation.size() < 1) {
			return result;
		}
		for(int i=0;i<listPrintStation.size();i++) {
			pmAutoPrintStation.setTemplateName(listPrintStation.get(i).getTemplateName());
			pmAutoPrintStation.setPrintName(listPrintStation.get(i).getPrintName());
			pmAutoPrintStation.setPrintType(listPrintStation.get(i).getPrintType());
			printStationDao.savePrintTask(pmAutoPrintStation);
			
		}
//		PmAutoPrintStation printStation = listPrintStation.get(0);
//		//打印vin标签
//		if(printStation.getPrint_vin_label()==1) {
//			// 获取打印机名称
//			List<PortalBaseLov> list = pbsi.api_queryListByParamKey1("VIN_LABEL");
//			if (list == null || list.size() < 1) {
//				result.setSuccess(false);
//				result.setMessage("无法获取打印机名称");
//				return result;
//			}
//		    printName = list.get(0).getOption_key();
//			//保存vin标签打印任务
//			pmAutoPrintStation.setPrintFunction("VIN_LABEL");
//			pmAutoPrintStation.setPrintType("VIN_LABEL");;
//			pmAutoPrintStation.setPrintName(printName);
//			printStationDao.savePrintTask(pmAutoPrintStation);
//		}
//		if(printStation.getPrint_loading_list()==1) {
//			// 获取打印机名称
//			List<PortalBaseLov> list = pbsi.api_queryListByParamKey1("AL_TCFNS");
//			if (list == null || list.size() < 1) {
//				result.setSuccess(false);
//				result.setMessage("无法获取打印机名称");
//				return result;
//			}
//		    printName = list.get(0).getOption_key();
//			//保存整车装车单打印任务
//			pmAutoPrintStation.setPrintFunction("AL_TCFNS");
//			pmAutoPrintStation.setPrintType("AL_TCFNS");
//			pmAutoPrintStation.setPrintName(printName);
//			printStationDao.savePrintTask(pmAutoPrintStation);
//		}
//		if(printStation.getPrint_cad_list()==1) {
//			// 获取打印机名称
//			List<PortalBaseLov> list = pbsi.api_queryListByParamKey1("AL_CAB");
//			if (list == null || list.size() < 1) {
//				result.setSuccess(false);
//				result.setMessage("无法获取打印机名称");
//				return result;
//			}
//		    printName = list.get(0).getOption_key();
//			//保存驾驶室打印任务
//			pmAutoPrintStation.setPrintFunction("AL_CAB");
//			pmAutoPrintStation.setPrintType("AL_CAB");
//			pmAutoPrintStation.setPrintName(printName);
//			printStationDao.savePrintTask(pmAutoPrintStation);
//		}
		
		return result;
	}

}
